// cluster.go
package rbac

import (
	//"fmt"

	"log"
	//"time"
	m "xwaf_admin/xadmin/src/models"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/tidwall/gjson"
)

type ClusterController struct {
	beego.Controller
}

//列表
func (this *ClusterController) List() {
	//id := this.GetString("id")

	if this.Ctx.Request.Method == "POST" {
		gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)
		datas, count := m.GetUserClusterListV2(gp.Map())
		this.Data["json"] = &map[string]interface{}{"code": 0, "msg": "", "count": count, "data": &datas}
		this.ServeJSON()
		return
	} else {
		username := this.GetString("username")
		clusterId := this.GetString("clusterId")
		page, _ := this.GetInt64("page")
		page_size, _ := this.GetInt64("limit")

		datas, count := m.GetUserClusterList(username, clusterId, page, page_size)
		this.Data["json"] = &map[string]interface{}{"code": 0, "msg": "", "count": count, "data": &datas}
		this.ServeJSON()
	}
}

//添加
func (this *ClusterController) Add() {
	u := m.UserCluster{}
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)

	if gp.Get("clusterId").Exists() {
		u.ClusterId = gp.Get("clusterId").String()
		u.Username = gp.Get("username").String()

		id, err := m.AddUserCluster(&u)
		if err == nil && id > 0 {
			log.Printf("[INFO] Add success")
			this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
		} else {
			log.Printf("[ERROR] Add Fail:%s", err)
			this.Ctx.WriteString(`{"code":-1,"msg":"error"}`)
		}
	} else {
		this.Data["json"] = &map[string]interface{}{"code": -1, "msg": "bodyParseFail"}
		this.ServeJSON()
	}
}

//删除
func (this *ClusterController) Delete() {
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")

	Id, _ := this.GetInt64("id")
	respid, err := m.DelUserClusterById(Id)
	if err == nil && respid > 0 {
		log.Printf("[INFO] Delsuccess")
		this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
	} else {
		log.Printf("[ERROR] Del Error:%s\n", err.Error())
		this.Ctx.WriteString(`{"code":-1,"msg":"Error"}`)
	}
}
